include $(TOPDIR)/rules.mk

PKG_NAME:=redsocks2
PKG_VERSION:=0.60
PKG_RELEASE:=1

PKG_SOURCE_PROTO:=git
PKG_SOURCE_URL:=https://github.com/semigodking/redsocks.git
PKG_SOURCE_SUBDIR:=$(PKG_NAME)-$(PKG_VERSION)
PKG_SOURCE_VERSION:=68a479e0720b84e8429b1037c5efa9b953c5bb6c
PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION)-$(PKG_SOURCE_VERSION).tar.gz

include $(INCLUDE_DIR)/package.mk

define Package/redsocks2
SECTION:=net
CATEGORY:=Network
SUBMENU:=Web Servers/Proxies
DEPENDS:=+libevent2 +libopenssl
TITLE:=Redirect any TCP connection to a SOCKS or HTTPS proxy server
endef

define Package/redsocks2/description
This is a modified version of original redsocks.
The name is changed to be REDSOCKS2 since this release to distinguish with original redsocks. 
This variant is useful for anti-GFW (Great Fire Wall).
endef

define Package/redsocks2/postinst
#!/bin/sh
( . /etc/uci-defaults/redsocks2 ) && rm -f /etc/uci-defaults/redsocks2
exit 0
endef

define Package/redsocks2/prerm
#!/bin/sh
uci -q batch <<-EOF >/dev/null
	delete firewall.redsocks2
	commit firewall
EOF
/etc/init.d/redsocks2 disable >/dev/null 2>&1
exit 0
endef

define Package/redsocks2/install
	$(INSTALL_DIR) $(1)/usr/bin
	$(INSTALL_BIN) $(PKG_BUILD_DIR)/redsocks2 $(1)/usr/bin
	$(INSTALL_DIR) $(1)/etc/redsocks2
	$(INSTALL_CONF) $(PKG_BUILD_DIR)/redsocks.conf.example $(1)/etc/redsocks2/redsocks.conf.example
	$(INSTALL_DIR) $(1)/etc/init.d
	$(INSTALL_BIN) ./files/redsocks2.init $(1)/etc/init.d/redsocks2
	$(INSTALL_DIR) $(1)/etc/config
	$(INSTALL_CONF) ./files/redsocks2.config $(1)/etc/config/redsocks2
	$(INSTALL_DIR) $(1)/etc/hotplug.d/iface
	$(INSTALL_BIN) ./files/redsocks2.hotplug $(1)/etc/hotplug.d/iface/90-redsocks2
	$(INSTALL_DIR) $(1)/etc
	$(INSTALL_BIN) ./files/redsocks2.firewall $(1)/etc/firewall.redsocks2
	$(INSTALL_DIR) $(1)/etc/uci-defaults
	$(INSTALL_BIN) ./files/redsocks2.uci $(1)/etc/uci-defaults/redsocks2
endef

$(eval $(call BuildPackage,redsocks2))
